package com.xgkj.diyiketang.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xgkj.diyiketang.ProtocolConst;
import com.xgkj.diyiketang.data.SellerInfo;
import com.xgkj.diyiketang.http.CouponHttpRequest;
import com.xgkj.diyiketang.http.CouponHttpResponse;
import com.xgkj.diyiketang.http.engine.CouponHttpBaseTask;

import java.util.ArrayList;
import java.util.List;

/**
 * 觅食列表或者详情
 * fx
 */
public class MiShiListOrDetailTask extends CouponHttpBaseTask {
    private int mSize;
    private int mPageNo;
    private String mAreaName;
    private String mType;
    private String mTradingAreaId;

    private static final String MOREVIDEOLIST = "businessList";

    public MiShiListOrDetailTask(int size, int pageNo, String areaName, String type, String tradingAreaid) {
        super(TaskType.MISHILIST_OR_DETAIL_TASK);
        mSize = size;
        mPageNo = pageNo;
        mAreaName = areaName;
        mType = type;
        mTradingAreaId = tradingAreaid;
    }

    @Override
    protected void processResponseData(CouponHttpResponse response) {
        JSONObject json = response.getBody();
        int currentPage = 0;
        int pageCount = 0;
        int nextPage = 0;
        List<SellerInfo> list = new ArrayList<SellerInfo>();
        if (json != null) {
            JSONObject jsonString = json.getJSONObject(MOREVIDEOLIST);
            if (jsonString != null) {
                currentPage = jsonString
                        .getIntValue(ProtocolConst.CURRENT_PAGE);
                pageCount = jsonString.getIntValue(ProtocolConst.PAGE_COUNT);
                nextPage = currentPage >= pageCount ? 0 : (currentPage + 1);
                list = JSON.parseArray(
                        jsonString.getString(ProtocolConst.LIST_TAG),
                        SellerInfo.class);
            }

        }
        notifyTaskCompleted(nextPage, list);
    }

    @Override
    protected void prepareRequest() {
        super.prepareRequest();
        if (mPhase == TASK_PHASE_INIT) {
            CouponHttpRequest request = new CouponHttpRequest(
                    RequestType.MISHILIST_OR_DETAIL_REQUEST);
            JSONObject json = new JSONObject();
            json.put("size", mSize);
            json.put("pageNo", mPageNo);
            json.put("areaName", mAreaName);
            json.put("type", mType);
            json.put("tradingAreaId",mTradingAreaId);
            request.setData(json.toString());
            request.setApiNo(ApiNo.MISHILIST_OR_DETAIL);
            addRequest(request);
        }
    }
}
